/*
 * PersonListModel.java
 *
 * Created on 28 May 2003, 09:19
 * Copyright (C) 2003, Velocityme Partnership
 */

package com.velocityme.client.gui;

import com.velocityme.valueobjects.ContactableValue;
import com.velocityme.valueobjects.ContactableValueToString;
import com.velocityme.valueobjects.DepartmentValue;
import com.velocityme.valueobjects.DepartmentValueToString;
import com.velocityme.valueobjects.PersonValue;
import com.velocityme.valueobjects.PersonValueToString;
import com.velocityme.valueobjects.RoleValue;
import com.velocityme.valueobjects.RoleValueToString;
import com.velocityme.valueobjects.StatusValue;
import com.velocityme.valueobjects.StatusValueToString;
import com.velocityme.valueobjects.UserValue;
import com.velocityme.valueobjects.UserValueToString;
import java.util.ArrayList;
import javax.swing.AbstractListModel;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeSet;

/**
 *
 * @author  Robert Crida Work
 */
public class ValueListModel extends AbstractListModel {
    private TreeSet m_valueSet;
    private Object[] m_valueArray;

    public ValueListModel(Collection p_values) {
        m_valueSet = new TreeSet();
        Iterator i = p_values.iterator();
        while(i.hasNext()) {
            Object obj = i.next();
            if (obj instanceof ContactableValue)
                m_valueSet.add(new ContactableValueToString((ContactableValue)obj));
            if (obj instanceof DepartmentValue)
                m_valueSet.add(new DepartmentValueToString((DepartmentValue)obj));
            if (obj instanceof PersonValue)
                m_valueSet.add(new PersonValueToString((PersonValue)obj));
            if (obj instanceof RoleValue)
                m_valueSet.add(new RoleValueToString((RoleValue)obj));
            if (obj instanceof StatusValue)
                m_valueSet.add(new StatusValueToString((StatusValue)obj));
            if (obj instanceof UserValue)
                m_valueSet.add(new UserValueToString((UserValue)obj));
        }
        m_valueArray = m_valueSet.toArray();
    }
    
    public ValueListModel(Object[] p_values) {
        m_valueSet = new TreeSet();
        for (int i = 0; i < p_values.length; i++) {
            Object obj = p_values[i];
            if (obj instanceof ContactableValue)
                m_valueSet.add(new ContactableValueToString((ContactableValue)obj));
            if (obj instanceof DepartmentValue)
                m_valueSet.add(new DepartmentValueToString((DepartmentValue)obj));
            if (obj instanceof PersonValue)
                m_valueSet.add(new PersonValueToString((PersonValue)obj));
            if (obj instanceof RoleValue)
                m_valueSet.add(new RoleValueToString((RoleValue)obj));
            if (obj instanceof StatusValue)
                m_valueSet.add(new StatusValueToString((StatusValue)obj));
            if (obj instanceof UserValue)
                m_valueSet.add(new UserValueToString((UserValue)obj));
        }
        m_valueArray = m_valueSet.toArray();
    }
    
    public ValueListModel() {
        m_valueSet = new TreeSet();
        m_valueArray = m_valueSet.toArray();
    }

    /** Returns the value at the specified index.
     * @param index the requested index
     * @return the value at <code>index</code>
     *
     */
    public Object getElementAt(int index) {
        return m_valueArray[index];
    }

    /**
     * Returns the length of the list.
     * @return the length of the list
     *
     */
    public int getSize() {
        return m_valueArray.length;
    }
    
    public void setItems(Collection p_personValue) {
        m_valueSet.clear();
        Iterator it = p_personValue.iterator();
        while(it.hasNext()) {
            Object obj = it.next();
            if (obj instanceof ContactableValue)
                m_valueSet.add(new ContactableValueToString((ContactableValue)obj));
            if (obj instanceof DepartmentValue)
                m_valueSet.add(new DepartmentValueToString((DepartmentValue)obj));
            if (obj instanceof PersonValue)
                m_valueSet.add(new PersonValueToString((PersonValue)obj));
            if (obj instanceof RoleValue)
                m_valueSet.add(new RoleValueToString((RoleValue)obj));
            if (obj instanceof StatusValue)
                m_valueSet.add(new StatusValueToString((StatusValue)obj));
            if (obj instanceof UserValue)
                m_valueSet.add(new UserValueToString((UserValue)obj));
        }
        m_valueArray = m_valueSet.toArray();
        fireIntervalAdded(this, 0, getSize());
    }

    public void add(Object[] vals) {
        for (int i = 0; i < vals.length; i++)
            m_valueSet.add(vals[i]);
        m_valueArray = m_valueSet.toArray();
        fireIntervalAdded(this, 0, getSize());
    }

    public void remove(Object[] vals) {
        for (int i = 0; i < vals.length; i++)
            m_valueSet.remove(vals[i]);
        m_valueArray = m_valueSet.toArray();
        fireIntervalRemoved(this, 0, getSize());
    }
    
    public Collection getCollection() {
        return new ArrayList(m_valueSet);
    }
    
}
